package com.situ.mall.controller.front;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.user.UserRegistryMessageHandler;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.situ.mall.entity.Cart;
import com.situ.mall.entity.Result;
import com.situ.mall.entity.User;
import com.situ.mall.service.ICartService;

@Controller
@RequestMapping("/cart")
public class FrontCartController {
	@Autowired
	private ICartService cartService;

	@RequestMapping("/insert")
	@ResponseBody
	public Result insert(Cart cart, HttpSession session) {
		User user = (User)session.getAttribute("user");
		cart.setUserId(user.getId());
		cart.setChecked(1);
		boolean isSuccess = cartService.insert(cart);
		if (isSuccess) {
			return Result.success("插入购物车成功");
		} else {
			return Result.error("插入购物车失败");
		}
	}
	
	@RequestMapping("/getCartListPage.shtml")
	public String getCartListPage(HttpSession session, Model model) {
		User user = (User)session.getAttribute("user");
		List<Cart> list = cartService.selectByUserId(user.getId());
		model.addAttribute("list", list);
		return "cart_list";
	}
	
	@RequestMapping("/deleteById")
	@ResponseBody
	public Result deleteById(Integer cartId) {
		boolean isSuccess = cartService.deleteById(cartId);
		if (isSuccess) {
			return Result.success("删除购物车成功");
		} else {
			return Result.error("删除购物车失败");
		}
	}
	
	//updateQuantity
	// update cart set quantity=quantity+num where id=12;
	@RequestMapping("/updateQuantity")
	@ResponseBody
	public Result updateQuantity(Integer cartId, Integer num) {
		boolean isSuccess = cartService.updateQuantity(cartId, num);
		if (isSuccess) {
			return Result.success("更新数量成功");
		} else {
			return Result.error("更新数量失败");
		}
	}
	
	@RequestMapping("/updateCheckStatus")
	@ResponseBody
	public Result updateCheckStatus(Integer cartId, Integer checked) {
		boolean isSuccess = cartService.updateCheckStatus(cartId, checked);
		if (isSuccess) {
			return Result.success("更新状态成功");
		} else {
			return Result.error("更新状态失败");
		}
	}
}
